package top.malaoshi.config;

import org.springframework.stereotype.Component;
import top.malaoshi.dao.GoodsMapper;
import top.malaoshi.entity.Goods;
import top.malaoshi.redis.RedisUtil;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

@Component
public class GoodsLikeHandler {
    @Resource
    private GoodsMapper mapper;

    @Resource
    private RedisUtil redisUtil;

    /**
     * 调用构造方法，实例化该类后，会自动调用该注解修饰的方法
     */
    @PostConstruct
    public void handle(){
        List<Goods> list=mapper.list();
        for(Goods goods:list){
            String id=goods.getId();
            int like=goods.getLikeNum();
            String redisKeyGoodsLike="goods_like_"+id;
            redisUtil.set(redisKeyGoodsLike,like);
        }
    }
}
